//
//  MainFirst_MagazineVideo_VC_WX.m
//  ZITHER
//
//  Created by weixin on 2018/10/30.
//  Copyright © 2018年 weixn. All rights reserved.
//

#import "MainFirst_MagazineVideo_VC_WX.h"
#import "SuperPlayer/SuperPlayerView+Private.h"
#import "UIView+Fade.h"



@interface MainFirst_MagazineVideo_VC_WX ()<SuperPlayerDelegate,UINavigationControllerBackButtonHandlerProtocol>
@property (nonatomic,strong)SuperPlayerView * playerView;
@end

@implementation MainFirst_MagazineVideo_VC_WX

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = UIColorBlack;
    [self addTouchView];
    [self setNav];
    [self setPlayer];
    
}

/** 添加点击区域 */
-(void)addTouchView{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
    view.backgroundColor = [UIColor clearColor];
    //    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    [self.view addSubview:view];
    /** 添加点击事件 */
    @weakify(self);
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
    [[tap rac_gestureSignal] subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
        @strongify(self);
        NSLog(@"=============================");
        SPDefaultControlView *view1 =  (SPDefaultControlView *)self.playerView.controlView;  if (x.state == UIGestureRecognizerStateRecognized) {
            
            if (SuperPlayerWindowShared.isShowing)
                return;
            
            if (view1.hidden) {
                [[self.playerView.controlView fadeShow] fadeOut:5];
            } else {
                [self.playerView.controlView fadeOut:0.2];
            }
        }
    }];
    view.userInteractionEnabled = YES;
    [view addGestureRecognizer:tap];
}



- (void)setModelJson:(HomePageDetailModelpgcJson *)modelJson{
    _modelJson = modelJson;
    [self setUI];

}

-(void)setNav{
    [self.baseNavigationView setHiddenNav:YES VC:self];
    /* TODO: ================= 导航栏箭头颜色 =================*/
    QMUIButton * btn = [[QMUIButton alloc]initWithFrame:CGRectMake(14,kStatusBarHeight+5,40,40)];
    UIImage *image = [UIImage imageNamed:@"common_back"];
    image =[image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    [btn setImage:image forState:UIControlStateNormal];
    [btn.imageView setTintColor:[UIColor whiteColor]];
    
    [self.view addSubview:btn];
    @weakify(self);
    [[btn rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        [self.navigationController popViewControllerAnimated:YES];
    }];
    
    self.tabBarController.tabBar.hidden = YES;
}

/// 全屏改变通知
- (void)superPlayerFullScreenChanged:(SuperPlayerView *)player{
    NSLog(@"全屏===================");
    SPDefaultControlView *view1 =  (SPDefaultControlView *)self.playerView.controlView;
    if (self.playerView.isFullScreen) {
        view1.backBtn.hidden = NO;
        view1.danmakuBtn.hidden = YES;
    }else{
        view1.backBtn.hidden = YES;
    }
}

/// 播放结束通知
-(void)superPlayerDidEnd:(SuperPlayerView *)player{
    NSLog(@"结束播放===================");
//    [self.playerView autoPlay];
    SPDefaultControlView *view =  (SPDefaultControlView *)self.playerView.controlView;

    view.repeatBtn.hidden = NO;
    self.playerView.playDidEnd = NO;
//    setPlayState
}
- (void)superPlayerBackAction:(SuperPlayerView *)player{
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.playerView pause];
//    [_playerView removeFromSuperview];
//    _playerView = nil;

}


-(void)setPlayer{
   
    NSLog(@"%@",self.playerView.controlView);
    SPDefaultControlView *view =  (SPDefaultControlView *)self.playerView.controlView;
    [view.fullScreenBtn setImage:[UIImage imageNamed:@"pgc_视频_全屏"] forState:UIControlStateNormal];
    view.backBtn.hidden = !view.backBtn.hidden;

    [RACObserve(view, danmakuBtn.hidden)subscribeNext:^(id  _Nullable x) {
        if (!view.danmakuBtn.hidden) {
            view.danmakuBtn.hidden = YES;
        }
    }];
    
}

-(void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = YES;
}

-(void)setUI{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0,200, kScreenW, 300)];
    [self.view addSubview:view];
    self.playerView.fatherView = view;
}
-(void)dealloc{
    _playerView = nil;
}

- (SuperPlayerView *)playerView{
    if (!_playerView) {
        _playerView = [[SuperPlayerView alloc] init];
        // 设置代理，用于接受事件
        _playerView.delegate = self;
        // 设置父View，_playerView会被自动添加到holderView下面
//        _playerView.fatherView = self.view;
        
        SuperPlayerModel *playerModel = [[SuperPlayerModel alloc] init];
        // 设置播放地址，直播、点播都可以
        playerModel.videoURL = self.modelJson.src;
        [_playerView.coverImageView sd_setImageWithURL:[NSURL URLWithString:self.modelJson.cover]];
        _playerView.coverImageView.userInteractionEnabled = NO;

        // 开始播放
        [_playerView playWithModel:playerModel];
    }
    return _playerView;
}




@end
